// 侧边栏多层列表
(function($) {
    $.expr[":"].Contains = function(a, i, m) {
        return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
    };
    function filterList(header, list) {
        //@header 头部元素
        //@list 无需列表
        //创建一个搜素表单
        var form = $("<form>").attr({
            "class":"filterform",
            action:"#"
        }), input = $("<input>").attr({
            "class":"filterinput",
            type:"text"
        });
        $(form).append(input).appendTo(header);
        $(input).change(function() {
            var filter = $(this).val();
            if (filter) {
                $matches = $(list).find("a:Contains(" + filter + ")").parent();
                $("li", list).not($matches).slideUp();
                $matches.slideDown();
            } else {
                $(list).find("li").slideDown();
            }
            return false;
        }).keyup(function() {
            $(this).change();
        });
    }
    $(function() {
        filterList($("#form"), $("#demo-list"));
    });
})(jQuery);


$(function(){
    layui.config({
        version: false,
        debug: false,
        base: 'layui/modules/'
    }).use('global'); //加载入口

    layui.use(['element', 'carousel','form', 'table'], function(){
        var table = layui.table;
        var element = layui.element;
        var carousel = layui.carousel;
        var form = layui.form;
        var layer = layui.layer;
        //建造实例
        carousel.render({
            elem: '#carousel',
            width: '94%',
            height: '20rem',
            arrow: 'hover'
        });
        // table.init('demo', {
        //     height: 315 //设置高度
        //     ,limit: 3 //注意：请务必确保 limit 参数（默认：10）是与你服务端限定的数据条数一致
        //     //支持所有基础参数
        // });
        //执行渲染
        // table.render({
        //     elem: '#demo' //指定原始表格元素选择器（推荐id选择器）
        //     ,height: 315 //容器高度
        //     ,limit: 3
        // });
    });

    jQuery("#jquery-accordion-menu").jqueryAccordionMenu();

    var main = $('body');
    var user_name = sessionStorage.getItem("user_name");
    var user_id = sessionStorage.getItem("user_id");
    var user_phone = sessionStorage.getItem("user_phone");
    var company_image = sessionStorage.getItem("company_image");
    var company_info = sessionStorage.getItem("company_info");
    var company_name = sessionStorage.getItem("company_name");
    var company_id = sessionStorage.getItem("company_id");
    var auth = sessionStorage.getItem("auth");
    var total_employees = sessionStorage.getItem("total_employees");
    var total_departments = sessionStorage.getItem("total_departments");

    $('.user_name').text(user_name);
    $('.company_name').text(company_name);
    $('.total_employees').text(total_employees+'人');
    $('.total_departments').text(total_departments+'个');

    // 判断主/子管理员
    if(auth == 0){
        $('.auth').text('总管理员');
    }else {
        $('.auth').text('子管理员');
        $('.s-setting .edit_company').css('display','none');
        $('.s-setting #setting-item-2').css('display','none');
        $('.s-setting #setting-item-3').css('display','none');
        $('.s-setting #name').attr('disabled',true);
        $('.s-setting #info').attr('disabled',true);
    }

    //初始化
    $(".s-pool .s-right").children().eq(0).css("display","block").siblings().css("display","none");
    $(".s-data .s-right").children().eq(0).css("display","block").siblings().css("display","none");
    $(".s-setting .s-right").children().eq(0).css("display","block").siblings().css("display","none");
    $(".s-home").siblings('section').css('display','none');

    $(".header-container div").click(function () {
        if($(this).attr('class') === 'home' || $(this).attr('class') === 'company_name'){
            $(this).siblings().css({'borderBottom':'none','color':'#000'});
            $('.s-' + $(this).attr('class')).css('display','block')
                .siblings().css('display','none');
        }else{
            $(this).css({'borderBottom':'0.2rem inset #3296FA','color':'#3296FA'})
                .siblings().css({'borderBottom':'none','color':'#000'});
            $('.s-' + $(this).attr('class')).css('display','block')
                .siblings().css('display','none');
        }
    });

    //用于控制左边导航栏
    $(".s-left .sub-menu-select-item").click(function () {
       $(".s-right"+" .s-"+$(this).attr("id")).css("display","block").siblings().css('display','none');
       $(this).addClass("s-left-first").siblings().removeClass("s-left-first");
    });

    // 点击首页-右上角的叉
    main.on("click", ".background_img i", function () {
        $('.background_img').css('display','none');
        $('#carousel').css('display','block');
    });

    // 点击标题栏--右上角的注销
    main.on("click", "#logout", function () {
        sessionStorage.clear();
        window.location.href='./index.html';
    });

    // 点击通讯录tab
    main.on("click", ".content", function (){
        $(".department_setting").css('display','none');
        $('.department_name').html(company_name);
        methods.get_company_structure(user_id);
        $('.company').append(company_name);
        methods.get_employees(company_id);
    });

    // 点击通讯录-编辑部门-立即修改按钮
    main.on("click", "#edit_department", function (){
        var new_department_name = $('#new_department_name').val();
        var department_id = $('#edit_department').attr('name');
        var parent_id = $('#department_name').val();
        if(new_department_name == ''){
            layer.msg('请输入新部门名称...');
        }else if(parent_id == '') {
            layer.msg('请选择上级部门...');
        }else if(department_id == parent_id){
            layer.msg('请选择正确的上级部门而不是本部门...');
        }else {
            methods.edit_department(department_id,new_department_name,parent_id);
        }
    });

    // 点击通讯录-编辑部门-删除部门按钮
    main.on("click", "#delete_department", function (){
        var department_id = $('#delete_department').attr('name');
        layer.open({
            title: '确认框',
            content: '是否确认删除该部门？'
            ,btn: ['确认', '取消']
            ,yes: function(index, layero){
                methods.delete_department(department_id);
            }
            ,btn2: function(index, layero){
                //return false 开启该代码可禁止点击该按钮关闭
            }
            ,cancel: function(){
                //return false 开启该代码可禁止点击该按钮关闭
            }
        });
    });

    // 点击通讯录-添加部门成员按钮
    main.on("click", "#add_employee_btn", function (){
        var employee_name = $('#employee_name').val();
        var employee_phone = $('#employee_phone').val();
        var superior_id = $('#superior_name').val();
        var employee_duty = $('#employee_duty').val();
        var department_id = $('#add_department').attr('name');
        if(employee_name == ''){
            layer.msg('请输入员工用户名...');
        }else if(employee_phone == ''){
            layer.msg('请输入员工手机号...');
        }else if(superior_id == ''){
            layer.msg('请选择员工上级...');
        }else if(employee_duty == ''){
            layer.msg('请输入员工职务...');
        }else {
            methods.add_employee(employee_name,employee_phone,company_id,department_id,employee_duty,superior_id);
            $('#add_employee').modal('hide');
            $('.modal-backdrop').css('display','none');
            $('body').removeClass('modal-open');
        }
    });

    // 点击通讯录-添加子部门按钮
    main.on("click", "#add_department", function (){
        var new_department = $('#new_department').val();
        var department_id = $('#add_department').attr('name');
        if(new_department == ''){
            layer.msg('请输入员工用户名...');
        }else {
            methods.add_department(department_id,new_department);
        }
    });

    // 点击通讯录-编辑员工按钮
    main.on("click", ".edit_employee", function (e){
        var employee_id = $(e.target).attr('name');
        $('#edit_employee_btn').attr('name',employee_id);
        $('#sub_employee_btn').attr('name',employee_id);
        methods.get_departmet(company_id);
        $('#employee_name2').val($(e.target).parent().parent().find("td").eq(0).html());
        $('#employee_phone2').val($(e.target).parent().parent().find("td").eq(1).html());
        $('#employee_duty2').val($(e.target).parent().parent().find("td").eq(2).html());
    });

    // 点击通讯录-立即修改员工按钮
    main.on("click", "#edit_employee_btn", function (e){
        var employee_id = $(e.target).attr('name');
        methods.get_departmet(company_id);
        var employee_phone = $('#employee_phone2').val();
        var superior_id = $('#superior_name3').val();
        var employee_duty = $('#employee_duty2').val();
        var department_id = $('#select_department3').val();
        if(department_id == ''){
            layer.msg('请选择员工的部门...');
        }else if(superior_id == ''){
            layer.msg('请选择员工上级...');
        }else if(employee_duty == ''){
            layer.msg('请输入员工职务...');
        }else {
            methods.edit_employee(employee_phone,company_id,department_id,employee_duty,superior_id);
            $('#edit_employee').modal('hide');
            $('.modal-backdrop').css('display','none');
            $('body').removeClass('modal-open');
        }
    });

    // 点击通讯录-查看下级员工按钮
    main.on("click", "#sub_employee_btn", function (e){
        var employee_id = $(e.target).attr('name');
        methods.get_departmet(company_id);
        methods.get_sub_employee(employee_id);
    });

    // 点击通讯录-删除员工按钮
    main.on("click", ".delete_employee", function (e){
        var employee_id = $(e.target).attr('name');
        layer.open({
            title: '确认框',
            content: '是否确认删除该员工？'
            ,btn: ['确认', '取消']
            ,yes: function(index, layero){
                methods.delete_employee(employee_id);
            }
            ,btn2: function(index, layero){
                //return false 开启该代码可禁止点击该按钮关闭
            }
            ,cancel: function(){
                //return false 开启该代码可禁止点击该按钮关闭
            }
        });
    });

    //点击工作池
    main.on("click", ".pool", function (){
        var company_id = sessionStorage.getItem("company_id");
        methods.get_user_list(company_id);
    });

    //点击工作池-1.用户查询
    main.on("click", "#pool-item-1", function (){
        var company_id = sessionStorage.getItem("company_id");
        methods.get_user_list(company_id);
    });

    //点击工作池-1.用户查询-搜索按钮
    main.on("click", ".s-pool-item-1 .search_btn", function (){
        var search_name = $('#search_user').val();
        if(search_name == ''){
            methods.get_user_list(company_id);
        }else{
            methods.search_user(company_id,search_name);
        }
    });

    //点击工作池-4.员工查询-查看按钮
    main.on("click", ".search_user_list .user_info", function (e){
        var search_user_id = $(e.target).attr('name');
        $('.sys_resume').attr('name',search_user_id);
        $('.search_reward').attr('name',search_user_id);
        $('.add_reward_btn').attr('name',search_user_id);
        $('#get_user_name').val('');
        $('#get_user_phone').val('');
        $('#get_user_department').val('');
        $('#get_user_duty').val('');
        $('#get_user_gender').val('');
        $('#get_user_birth').val('');
        methods.get_user_info(search_user_id);
    });

    //点击工作池-4.员工查询-加入按钮
    main.on("click", ".search_user_list .add_user", function (e){
        var employee_id = $(e.target).attr('name');
        $('#add_user_btn').attr('name',employee_id);
        $('#user_name').val($(e.target).parent().parent().find("td").eq(0).html());
        $('#user_phone').val($(e.target).parent().parent().find("td").eq(1).html());
        methods.get_departmet(company_id);
        methods.get_employees(company_id);
    });

    //点击工作池-4.员工查询-立即添加按钮
    main.on("click", "#add_user_btn", function (e){
        var employee_id = $(e.target).attr('name');
        var employee_name = $('#user_name').val();
        var employee_phone = $('#user_phone').val();
        var superior_id = $('#user_superior').val();
        var employee_duty = $('#user_duty').val();
        var department_id = $('#select_department').val();
        if(department_id == ''){
            layer.msg('请选择员工所在部门...');
        }else if(superior_id == ''){
            layer.msg('请选择员工上级...');
        }else if(employee_duty == ''){
            layer.msg('请输入员工职务...');
        }else {
            methods.add_employee(employee_name,employee_phone,company_id,department_id,employee_duty,superior_id);
            $('#add_user').modal('hide');
            $('.modal-backdrop').css('display','none');
            $('body').removeClass('modal-open');
            methods.get_user_list(company_id);
        }
    });

    //点击工作池-4.员工查询-删除按钮
    main.on("click", ".search_user_list .delete_user", function (e){
        var employee_id = $(e.target).attr('name');
        layer.open({
            title: '确认框',
            content: '是否确认删除该员工？'
            ,btn: ['确认', '取消']
            ,yes: function(index, layero){
                methods.delete_employee(employee_id);
                methods.get_user_list(company_id);
            }
            ,btn2: function(index, layero){
                //return false 开启该代码可禁止点击该按钮关闭
            }
            ,cancel: function(){
                //return false 开启该代码可禁止点击该按钮关闭
            }
        });
    });

    //点击工作池-4.员工查询-系统简历按钮
    main.on("click", ".search_user_list .sys_resume", function (e){
        var search_user_id = $(e.target).attr('name');
        methods.get_sys_resume(search_user_id);
    });

    //点击工作池-4.员工查询-奖惩记录按钮
    main.on("click", ".search_user_list .search_reward", function (e){
        var search_user_id = $(e.target).attr('name');
        methods.get_reward(search_user_id);
    });

    //点击工作池-4.员工查询-确认添加奖惩按钮
    main.on("click", ".add_reward_btn", function (e){
        var content = $('#content').val();
        var is_reward = $('#is_reward').val();
        var search_user_id = $(e.target).attr('name');
        if(content == ''){
            layer.msg('请输入奖惩内容...');
        }else if(is_reward == ''){
            layer.msg('请选择奖惩类别...');
        }else{
            methods.add_reward(search_user_id,content,user_id,is_reward);
        }
    });

    //点击工作池-2.公司查询
    main.on("click", "#pool-item-2", function (){
        methods.get_company_list('');
    });

    //点击工作池-2.公司查询-搜索按钮
    main.on("click", ".s-pool-item-2 .search_btn", function (){
        var search_name = $('#search_company').val();
        methods.get_company_list(search_name);
    });

    // 点击企业数据tab
    main.on("click", ".data", function (){
        $('#total_employees').text(total_employees);
        methods.get_away_sub_employee(company_id);
        methods.draw_use_employee_table(company_id);
    });

    // 点击企业设置tab
    main.on("click", ".setting", function (){
        $('#name').val(company_name);
        $('#info').val(company_info);
    });

    // 点击设置-1.企业信息-提交修改
    main.on("click", ".edit_company", function (){
        var new_company_name = $("#name").val();
        var new_company_image = '';
        var new_company_info = $("#info").val();
        if(new_company_name == ''){
            layer.msg('公司名称不能为空，请重新输入...');
            $('#name').val(company_name);
        }else{
            methods.edit_company(company_id, new_company_name, new_company_image, new_company_info);
            company_name = new_company_name;
            company_info = new_company_info;
            $('.company_name').text(company_name);
        }
        return false;
    });

    // 点击设置-2.更换主管理员
    main.on("click", "#setting-item-2", function (){
        $('#manager_name').val(user_name);
        $('#manager_phone').val(user_phone);
        methods.get_employees(company_id);
    });

    // 点击设置-2.更换主管理员-提交修改
    main.on("click", ".edit_main_manager", function (){
        var manager_id = $("#selsect_main_manager").val();
        if(manager_id == ''){
            layer.msg('请选择新企业主管理员...');
        }else {
            layer.open({
                title: '确认框',
                content: '修改完成后，您将成为公司内的普通员工，且退出当前系统管理系统，是否确认修改？'
                ,btn: ['确认', '取消']
                ,yes: function(index, layero){
                    methods.edit_main_manager(company_id, user_id, manager_id);
                }
                ,btn2: function(index, layero){
                    //return false 开启该代码可禁止点击该按钮关闭
                }
                ,cancel: function(){
                    //return false 开启该代码可禁止点击该按钮关闭
                }
            });
        }
    });

    // 点击设置-3.设置子管理员
    main.on("click", "#setting-item-3", function (){
        methods.get_sub_manager(company_id);
        methods.get_employees(company_id);
    });

    // 点击设置-3.设置子管理员-添加子管理员
    main.on("click", ".add_sub_manager", function (){
        $('.s-setting-item-3 .layui-form').css('display','block');
        methods.get_sub_manager(company_id);
    });

    // 点击设置-3.设置子管理员-确认添加
    main.on("click", "#add_sub_manager", function (){
        var manager_id = $("#selsect_sub_manager").val();
        methods.add_sub_manager(manager_id);
        methods.get_sub_manager(company_id);
    });
});


var methods = {
    //重新渲染表单
    renderForm:function (){
        layui.use('form', function(){
            var form = layui.form;
            form.render('select','selFilter');
        });
    },

    // 获取公司一级部门
    get_company_structure: function (user_id) {
        $.ajax({
            type: 'get',
            url: 'http://106.15.196.50:8888/company_structure?user_id='+user_id,
            timeout: 6000,
            dataType: 'json',
            success: function(json){
                $('.department_name').html(sessionStorage.getItem("company_name"));
                if(json.code == 200){
                    $('#delete_department').attr('name',json.data.department_id);
                    $('.add_sub_department').attr('name',json.data.department_id);
                    $('#add_department').attr('name',json.data.department_id);
                    if(json.msg == '获取公司一级部门...'){
                        $('#demo-list').html('<li class="active" name=0 onclick="methods.click_department(this)"><a href="#" class="company"><i class="fa fa-home"></i></a></li>');
                        $('.company').append(sessionStorage.getItem("company_name"));
                        for (var i=0;i < json.data.sub_department.length;i++){
                            var department_name=json.data.sub_department[i].department_name;
                            var department_id=json.data.sub_department[i].department_id;
                            if (json.data.sub_department[i].sum_sub_department > 0){
                                $('#demo-list').append('<li name='+department_id+' onclick="methods.click_department(this)"><a href="#">'+department_name+'</a><ul class="submenu"></ul></li>');
                            }else{
                                $('#demo-list').append('<li name='+department_id+' onclick="methods.click_department(this)"><a href="#">'+department_name+'</a></li>');
                            }
                        }
                        $('.sub_department tbody').html(template('sub_departmrnt',json.data.sub_department));
                        if (json.data.employee.length == 0){
                            $('.employee tbody').html('该部门还未有成员...');
                        }else{
                            $('.employee tbody').html(template('employee',json.data.employee));
                        }
                    }else {
                        $('.sub_department tbody').html('该公司还未有子部门...');
                        if (json.data.employee.length == 0){
                            $('.employee tbody').html('该部门还未有成员...');
                        }else{
                            $('.employee tbody').html(template('employee',json.data.employee));
                        }
                    }
                }else {
                    layer.open({
                        title: '错误',
                        content: json.msg
                    });
                }
            },
            error: function(){
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        });
    },

    // 获取公司所有部门
    get_departmet:function (company_id) {
        $.ajax({
            type: 'get',
            url: 'http://106.15.196.50:8888/add_sub_department?company_id=' + company_id,
            timeout: 6000,
            dataType: 'json',
            success: function (json) {
                $('#department_list select').html(template('department',json.data.department));
                methods.renderForm();
                $('#department_list2 select').html(template('department',json.data.department));
                methods.renderForm();
                $('#department_list3 select').html(template('department',json.data.department));
                methods.renderForm();
            },
            error: function () {
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        });
    },

    // 点击部门获取子部门和部门成员
    get_department_structure:function (department_id) {
        $.ajax({
            type: 'post',
            url: 'http://106.15.196.50:8888/company_structure/',
            timeout: 6000,
            data: {
                department_id: department_id
            },
            dataType: 'json',
            success: function(json){
                if(json.code == 200) {
                    if (json.data.sub_department.length == 0){
                        $('.sub_department tbody').html('该部门还未有子部门...');
                    }else{
                        $('.sub_department tbody').html(template('sub_departmrnt',json.data.sub_department));
                    }
                    if (json.data.employee.length == 0){
                        $('.employee tbody').html('该部门还未有成员...');
                    }else{
                        $('.employee tbody').html(template('employee',json.data.employee));
                    }
                    methods.get_departmet(sessionStorage.getItem("company_id"));
                    // $('#department_name').val(json.data.parent_id);
                    // methods.renderForm();
                }
            },
            error: function(){
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        })
    },

    // 点击部门获取子部门和部门成员
    click_department:function (that) {
        $("#demo-list li.active").removeClass("active");
        $(that).addClass("active");
        var department_id = $(that).attr('name');
        $('.department_name').html($(that).text());
        $('#old_department_name').val($(that).text());
        $('#edit_department').attr('name',department_id);
        $('#delete_department').attr('name',department_id);
        $('.add_sub_department').attr('name',department_id);
        $('#add_department').attr('name',department_id);
        if (department_id == 0){
            $(".department_setting").css('display','none');
            $('.department_name').html(sessionStorage.getItem("company_name"));
            methods.get_company_structure(sessionStorage.getItem("user_id"));
            $('#demo-list').html('<li class="active" name=0 onclick="methods.click_department(this)"><a href="#" class="company"><i class="fa fa-home"></i></a></li>');
            $('.company').append(sessionStorage.getItem("company_name"));
        }else {
            $(".department_setting").css('display','inline');
            methods.get_department_structure(parseInt(department_id));
        }
    },

    // 更新当前部门
    edit_department:function (department_id,department_name,parent_department_id) {
        $.ajax({
            type: 'post',
            url: 'http://106.15.196.50:8888/edit_department/',
            timeout: 6000,
            data: {
                department_id: department_id,
                department_name: department_name,
                parent_department_id: parent_department_id
            },
            dataType: 'json',
            success: function(json){
                layer.msg(json.msg);
                if(json.code == 200) {
                    $('#department_setting').modal('hide');
                    methods.get_company_structure(sessionStorage.getItem("user_id"));
                    $('.department_name').html(department_name);
                }
            },
            error: function(){
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        })
    },

    // 删除当前部门
    delete_department:function (department_id) {
        $.ajax({
            type: 'get',
            url: 'http://106.15.196.50:8888/edit_department?department_id=' + department_id,
            timeout: 6000,
            dataType: 'json',
            success: function (json) {
                layer.msg(json.msg);
                if(json.code == 200){
                    $('#department_setting').modal('hide');
                    methods.get_company_structure(sessionStorage.getItem("user_id"));
                }
            },
            error: function () {
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        });
    },

    // 添加子部门
    add_department:function (department_id,department_name) {
        $.ajax({
            type: 'post',
            url: 'http://106.15.196.50:8888/add_sub_department/',
            timeout: 6000,
            data: {
                department_id: department_id,
                sub_department_name: department_name
            },
            dataType: 'json',
            success: function(json){
                layer.msg(json.msg);
                if(json.code == 200) {
                    $('#add_sub_department').modal('hide');
                    methods.get_company_structure(sessionStorage.getItem("user_id"));
                    methods.get_department_structure(json.data.sub_department_id);
                    $('.department_name').html(json.data.sub_department_name);
                }
            },
            error: function(){
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        })
    },

    // 添加部门成员
    add_employee:function (employee_name,employee_phone,company_id,department_id,employee_duty,superior_id) {
        $.ajax({
            type: 'post',
            url: 'http://106.15.196.50:8888/add_employee/',
            timeout: 6000,
            data: {
                name: employee_name,
                phone: employee_phone,
                company_id: company_id,
                department_id: department_id,
                duty: employee_duty,
                superior_id: superior_id
            },
            dataType: 'json',
            success: function(json){
                layer.msg(json.msg);
                if(json.code == 200) {
                    if(department_id == sessionStorage.getItem("company_id")){
                        methods.get_company_structure(sessionStorage.getItem("user_id"));
                    }else {
                        methods.get_department_structure(department_id);
                    }
                }
            },
            error: function(){
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        })
    },

    // 删除员工
    delete_employee:function (user_id) {
        $.ajax({
            type: 'get',
            url: 'http://106.15.196.50:8888/edit_employee?id=' + user_id,
            timeout: 6000,
            dataType: 'json',
            success: function (json) {
                layer.msg(json.msg);
                if(json.code == 200) {
                    methods.get_user_list(sessionStorage.getItem("company_id"));
                    var department_id = $('.add_sub_department').attr('name');
                    if(department_id == sessionStorage.getItem("company_id")){
                        methods.get_company_structure(sessionStorage.getItem("user_id"));
                    }else {
                        methods.get_department_structure(department_id);
                    }
                }
            },
            error: function () {
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        });
    },

    // 编辑部门成员
    edit_employee:function (phone,company_id,department_id,duty,superior_id) {
        $.ajax({
            type: 'post',
            url: 'http://106.15.196.50:8888/edit_employee/',
            timeout: 6000,
            data: {
                phone: phone,
                company_id: company_id,
                department_id: department_id,
                duty: duty,
                superior_id: superior_id
            },
            dataType: 'json',
            success: function(json){
                layer.msg(json.msg);
                if(json.code == 200) {
                    if($('.add_sub_department').attr('name') == sessionStorage.getItem("company_id")){
                        methods.get_company_structure(sessionStorage.getItem("user_id"));
                    }else {
                        methods.get_department_structure($('.add_sub_department').attr('name'));
                    }
                }
            },
            error: function(){
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        })
    },

    // 获取下级员工
    get_sub_employee:function (user_id) {
        $.ajax({
            type: 'post',
            url: 'http://106.15.196.50:8888/get_sub_employee/',
            timeout: 6000,
            data: {
                user_id: user_id,
            },
            dataType: 'json',
            success: function(json){
                if(json.code == 200) {
                    $('#sub_employee tbody').html(template('employee',json.data.sub_employee));
                }else{
                    $('#sub_employee tbody').html(json.msg);
                }
            },
            error: function () {
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        })
    },

    // 获取平台用户列表
    get_user_list:function (company_id) {
        $.ajax({
            type: 'get',
            url: 'http://106.15.196.50:8888/search_user?company_id=' + company_id,
            timeout: 6000,
            dataType: 'json',
            success: function (json) {
                $('.search_user_list div').html(template('search_user_list',json.data.company));
            },
            error: function () {
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        });
    },

    // 根据姓名搜索用户
    search_user:function (company_id,name) {
        $.ajax({
            type: 'post',
            url: 'http://106.15.196.50:8888/search_user/',
            timeout: 6000,
            data: {
                company_id: company_id,
                name: name
            },
            dataType: 'json',
            success: function(json){
                if(json.code == 200) {
                    $('.search_user_list div').html(template('search_user_list',json.data.company));
                }else{
                    $('.search_user_list div').html(json.msg);
                }
            },
            error: function () {
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        })
    },

    // 查看员工信息
    get_user_info:function (user_id) {
        $.ajax({
            type: 'post',
            url: 'http://106.15.196.50:8888/get_user/',
            timeout: 6000,
            data: {
                user_id: user_id
            },
            dataType: 'json',
            success: function(json){
                if(json.code == 200) {
                    $('#get_user_name').val(json.data.name);
                    $('#get_user_phone').val(json.data.phone);
                    $('#get_user_gender').val(json.data.gender);
                    $('#get_user_birth').val(json.data.birth);
                    if(json.data.employee){
                        $('.get_user_department').css('display','block');
                        $('.get_user_duty').css('display','block');
                        $('.search_reward').css('display','inline-block');
                        $('#get_user_department').val(json.data.department_name);
                        $('#get_user_duty').val(json.data.duty);
                        $('#select_department3').val(json.data.department_id);
                        methods.renderForm();
                        $('#superior_name3').val(json.data.superior_id);
                        methods.renderForm();

                    }else {
                        $('.get_user_department').css('display','none');
                        $('.get_user_duty').css('display','none');
                        $('.search_reward').css('display','none');
                    }
                    if(json.data.resume_image == '' || json.data.resume_image == null){
                        $('.get_resume').css('display','none');
                    }else{
                        $('.get_resume').css('display','inline-block');
                        $('#layer-photos-demo1 img').attr("layer-src",json.data.resume_image);
                        $('#layer-photos-demo1 img').attr("src",json.data.resume_image);
                        layer.photos({
                            photos: '#layer-photos-demo1'
                            ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                        });
                    }
                }
            },
            error: function () {
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        })
    },

    // 获取系统历史简历列表
    get_sys_resume:function (user_id) {
        $.ajax({
            type: 'get',
            url: 'http://106.15.196.50:8888/edit_resume?user_id=' + user_id,
            timeout: 6000,
            dataType: 'json',
            success: function (json) {
                if(json.code == 200) {
                    $('#sys_resume .modal-body div').html(template('sys_resume_info',json.data.sys_resume));
                }else{
                    $('#sys_resume .modal-body div').html(json.msg);
                }
            },
            error: function () {
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        });
    },

    // 获取奖惩列表
    get_reward:function (user_id) {
        $.ajax({
            type: 'get',
            url: 'http://106.15.196.50:8888/reward_punishment?user_id=' + user_id,
            timeout: 6000,
            dataType: 'json',
            success: function (json) {
                if(json.code == 200) {
                    $('#reward_info .modal-body div').html(template('reward_info_list',json.data.away_employee));
                }else{
                    $('#reward_info .modal-body div').html(json.msg);
                }
            },
            error: function () {
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        });
    },

    // 添加奖惩
    add_reward:function (user_id,content,editor_id,is_reward) {
        $.ajax({
            type: 'post',
            url: 'http://106.15.196.50:8888/reward_punishment/',
            timeout: 6000,
            data: {
                user_id: user_id,
                content: content,
                editor_id: editor_id,
                is_reward: is_reward
            },
            dataType: 'json',
            success: function(json){
                if(json.code == 200) {
                    layer.msg(json.msg);
                    var search_user_id = $('.search_user_list .search_reward').attr('name');
                    methods.get_reward(search_user_id);
                    $('#add_reward').modal('hide');
                }
            },
            error: function () {
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        })
    },

    // 获取公司列表/根据公司名搜索公司
    get_company_list:function (company_name) {
        $.ajax({
            type: 'post',
            url: 'http://106.15.196.50:8888/search_company/',
            timeout: 6000,
            data: {
                company_name: company_name
            },
            dataType: 'json',
            success: function(json){
                if(json.code == 200) {
                    $('.search_company_list div').html(template('search_company_list',json.data.company));
                }else{
                    $('.search_company_list div').html(json.msg);
                }
            },
            error: function () {
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        })
    },

    // 获取离职员工
    get_away_sub_employee:function (company_id) {
        $.ajax({
            type: 'get',
            url: 'http://106.15.196.50:8888/get_sub_employee?company_id='+company_id,
            timeout: 6000,
            dataType: 'json',
            success: function (json) {
                if (json.code == 200) {
                    $('#sum_away_employee').text('目前离职人数:'+json.data.sum_away_employee);
                    $('.away_employee tbody').html(template('away_employee',json.data.away_employee));
                } else {
                    $('.s-data .away_employee').html('<h6>还未有员工离职...</h6>');
                }
            },
            error: function () {
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        })
    },

    // 绘制每日登录的员工人数列表
    draw_use_employee_table:function (company_id) {
        $.getJSON('http://106.15.196.50:8888/company_login?company_id='+company_id, function (data) {
            if(data.code == 200){
                data = data.data.use_employee;
                Highcharts.stockChart('c1', {
                    title: {
                        text: '员工使用情况'
                    },
                    series: [{
                        name: '使用人数',
                        data: data
                    }]
                });
            }else if(data.code == 400){
                alert(data.msg);
                return false;
            }
            else {
                alert('获取个人信用数据失败！');
                return false;
            }
        });
    },

    // 修改企业信息
    edit_company:function (company_id, company_name, company_image, company_info) {
        $.ajax({
            type: 'post',
            url: 'http://106.15.196.50:8888/company_setting/',
            timeout: 6000,
            data: {
                company_id: company_id,
                name: company_name,
                image: company_image,
                info: company_info
            },
            dataType: 'json',
            success: function (json) {
                sessionStorage.setItem("company_name",json.data.name);
                sessionStorage.setItem("company_info",json.data.info);
                sessionStorage.setItem("company_image",json.data.image);
                layer.msg(json.msg);
            },
            error: function () {
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        });
    },

    // 获取公司员工
    get_employees:function (company_id) {
        $.ajax({
            type: 'get',
            url: 'http://106.15.196.50:8888/search_employee?company_id='+company_id,
            timeout: 6000,
            dataType: 'json',
            success: function(json){
                $('#superior_list select').html(template('superior',json.data.company));
                methods.renderForm();
                $('#superior_list2 select').html(template('superior',json.data.company));
                methods.renderForm();
                $('#superior_list3 select').html(template('superior',json.data.company));
                methods.renderForm();
                $('.selsect_main_manager select').html(template('employees',json.data.company));
                methods.renderForm();

                for (var i=0; i<json.data.company.length; i++){
                    if (json.data.company[i].id == sessionStorage.getItem("user_id")){
                        json.data.company.splice(i,1);
                    }
                }
                $('.selsect_sub_manager select').html(template('manager',json.data.company));
                methods.renderForm();
            },
            error: function(){
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        });
    },

    // 修改企业主管理员
    edit_main_manager:function (company_id, user_id, manager_id) {
        $.ajax({
            type: 'post',
            url: 'http://106.15.196.50:8888/edit_manager/',
            timeout: 6000,
            data: {
                company_id: company_id,
                user_id: user_id,
                manager_id: manager_id
            },
            dataType: 'json',
            success: function (json) {
                layer.msg(json.msg);
                sessionStorage.clear();
                window.location.href='./index.html';
            },
            error: function () {
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        });
    },

    // 获取子管理员列表
    get_sub_manager:function (company_id) {
        $.ajax({
            type: 'get',
            url: 'http://106.15.196.50:8888/edit_sub_manager?company_id='+company_id,
            timeout: 6000,
            dataType: 'json',
            success: function(json){
                if(json.code == 200){
                    $('.s-setting-item-3 tbody').html(template('sub_manager',json.data.sub_manager));
                }else {
                    $('.s-setting-item-3 tbody').html(json.msg);
                }
            },
            error: function(){
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        });
    },

    // 添加子管理员
    add_sub_manager:function (user_id) {
        $.ajax({
            type: 'post',
            url: 'http://106.15.196.50:8888/add_sub_manager/',
            timeout: 6000,
            data: {
                user_id: user_id
            },
            dataType: 'json',
            success: function (json) {
                layer.msg(json.msg);
            },
            error: function () {
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        });
    },

    // 删除子管理员
    delete_sub_manager_btn:function (that) {
        var sub_manager_id = $(that).attr('name');
        $.ajax({
            type: 'post',
            url: 'http://106.15.196.50:8888/edit_sub_manager/',
            timeout: 6000,
            data: {
                user_id: sub_manager_id
            },
            dataType: 'json',
            success: function (json) {
                layer.msg(json.msg);
                methods.get_sub_manager(sessionStorage.getItem("company_id"));
            },
            error: function () {
                layer.open({
                    title: '错误',
                    content: '请求服务器错误，请重试...'
                });
            }
        });
    }
};